var countdown = 90;

function checkInputRegister(){
	if(!validate_mobile() || !validate_pwd() 
			|| !validate_code() || !validate_smscode() || !validate_protocal()){
		return false;
	}
	return true;
}

var err_info = "";

function checkInputLogin(){
	err_info = "";
	if(!validate_mobile() || !validate_pwd() || !validate_code()){
		return false;
	}
	return true;
}

function checkInputForget(){
	err_info = "";
	if(!validate_mobile() || !validate_code() 
			|| !validate_smscode() || !validate_pwd()  || !validate_cpwd()){
		return false;
	}
	return true;
}

function changeImgCode(){
	$(".imgcode").html('<img src="imgCode/index?t='+Math.random()+'" style="margin-top: -3px;margin-left: 5px;width:104px;height:37px;"/>');
}

function timer(){
	if (countdown == 0) {
		$(".btn-sms").text(" 免费获取校验码 ");
		$(".btn-sms-f").text(" 免费获取校验码 ");
		countdown = 90;
	} else {
		$(".btn-sms").text("剩余"+countdown +"秒可重发");
		$(".btn-sms-f").text("剩余"+countdown +"秒可重发");
		countdown--;
		setTimeout(function() {
			timer();
		}, 1000);
	}
}

function getSmsCode(type){
	if(!validate_mobile() || !validate_code()){
		return;
	}
	
	countdown = 90;
	var mobile = $("#mobile").val();
	$.getJSON("notify/getSmsCode?mobile="+mobile+"&type="+type,function(result){
		if(result.status == 200){
			timer();//发送成功，等待90秒
		}else if(result.status == 10002){
			zeroModal.error('请求过于频繁，手机号已被锁定，明日自动解锁!');
		}else if(result.status == 201){
			zeroModal.alert(result.data);
		}else{
			$("#err").html("系统繁忙，请稍后再试");//用于找回密码
			$("#err-info").css("display","block");
			
			$("#sms-err").html("系统繁忙，请稍后再试");//用户用户注册
		}
	});
}

var wait = 3;
var zurl = "";
function timeOut(){  
    if(wait==0){  
        if(!isEmpty(zurl)){
        	window.location = zurl;
        }  
    }else{                    
        setTimeout(function(){  
            wait--;  
            timeOut();  
        },1000)  
    }  
}

function register(){ 
	if(checkInputRegister()){
		var mobile = $("#mobile").val();
	    var pwd = $("#pwd").val();
	    var imgcode = $("#code").val();
	    var smscode = $("#sms").val();
	    
		var pdata = {"mobile":mobile,"imgcode":imgcode,"smscode":smscode,"pwd":pwd};
		$.ajax({
			url: "sell/register",
			data: JSON.stringify(pdata),
			contentType: "application/json",
			dataType: 'json',
			type: "POST",
			success: function (result) { 
				if(result.status == 200){
					wait = 3;
					zurl = result.data;
					timeOut();
				    zeroModal.success({
				    	content: '注册成功啦~，马上申请贷款吧！',
			            contentDetail: '3s后自动跳转，点击确认可直接进入~',
			            okFn: function() {
			            	window.location = zurl;
			            }
				    });
				}else if(result.status == 20001){
					zeroModal.error(result.data);
				}else{
					zeroModal.alert(result.data);
					changeImgCode();
				}
	        }
		});
	}
}

function login(){ 
	if(checkInputLogin()){
		var mobile = $("#mobile").val();
	    var pwd = $("#pwd").val();
	    var imgcode = $("#code").val();
	    
		var pdata = {"mobile":mobile,"imgcode":imgcode,"pwd":pwd};
		$.ajax({
			url: "sell/login",
			data: JSON.stringify(pdata),
			contentType: "application/json",
			dataType: 'json',
			type: "POST",
			success: function (result) { 
				if(result.status == 200){
					window.location = result.data;
				}else if(result.status == 20001){
					zeroModal.error(result.data);
				}else{
					$("#err").html(result.data);
					$("#err-info").css("display","block");
				}
	        }
		});
	}
}

function forget(){ 
	if(checkInputForget()){
		var mobile = $("#mobile").val();
	    var pwd = $("#pwd").val();
	    var imgcode = $("#code").val();
	    var smscode = $("#sms").val();
	    
		var pdata = {"mobile":mobile,"imgcode":imgcode,"smscode":smscode,"pwd":pwd};
		$.ajax({
			url: "sell/forget",
			data: JSON.stringify(pdata),
			contentType: "application/json",
			dataType: 'json',
			type: "POST",
			success: function (result) { 
				if(result.status == 200){
					wait = 3;
					zurl = result.data;
					timeOut();
				    zeroModal.success({
				    	content: '密码找成功啦~，继续操作吧！',
			            contentDetail: '3s后自动跳转，点击确认可直接进入~',
			            okFn: function() {
			            	window.location = zurl;
			            }
				    });
				}else if(result.status == 20001){
					zeroModal.error(result.data);
				}else{
					$("#err").html(result.data);
					$("#err-info").css("display","block");
					changeImgCode();
				}
	        }
		});
	}
}

